﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Framework.Reflection.Core
{
    class DummyMethodInfo : DummyMemberInfo, IDummyMethodInfo
    {
        bool isPrivate = false;
        bool isPublic = false;
        bool isStatic = false;
        bool isVirtual = false;
        IDummyType _value;
        List<DummyParameterInfo> parameters = new List<DummyParameterInfo>();

        [ReturnType]
        public IDummyType ReturnType
        {
            get { return _value; }
            set
            {
                if (!Verification.Verifies(value).IsNotNullOrEmpty().Result())
                    throw Exceptions.VerificationFailedException(value);

                _value = value;
            }
        }

        [IsPrivate]
        public bool IsPrivate
        {
            get { return isPrivate; }
            set { isPrivate = value; }
        }

        [IsPublic]
        public bool IsPublic
        {
            get { return isPublic; }
            set { isPublic = value; }
        }

        [IsStatic]
        public bool IsStatic
        {
            get { return isStatic; }
            set { isStatic = value; }
        }

        [IsVirtual]
        public bool IsVirtual
        {
            get { return isVirtual; }
            set { isVirtual = value; }
        }

        public IDummyParameterInfo[] GetParameters()
        {
            return parameters.ToArray();
        }

        public override bool Verify()
        {
            base.Verify();

            foreach (IDummyType att in GetCustomAttributes())
                att.Verify();

            return true;
        }

        public override void Dispose()
        {
            this._value = null;
            this.parameters.Clear();
            base.Dispose();
        }

        public override string ToString()
        {
            if (ReturnType == null)
                return this.Name;

            StringBuilder builder = new StringBuilder();
            builder.Append(ReturnType.ToString()).Append(" ");
            builder.Append(this.Name);

            if (parameters.Count == 0)
                return builder.Append("()").ToString();

            builder.Append("(");
            foreach (DummyParameterInfo para in parameters)
            {
                builder.Append(para.ToString()).Append(",");
            }
            builder.Remove(builder.Length - 1, 1);
            builder.Append(")");
            return builder.ToString();
        }

        [CreateParameter]
        public IDummyParameterInfo CreateParameter(string name, int position)
        {
            if (string.IsNullOrEmpty(name))
                throw Exceptions.VerificationFailedException(name, position);

            DummyParameterInfo parameter = new DummyParameterInfo(name, position);
            parameter.DeclaringType = null;
            parameters.Add(parameter);
            return parameter;
        }

        [LastParameter]
        internal IDummyParameterInfo LastParameter()
        {
            return parameters[parameters.Count - 1];
        }

        internal class IsPrivateAttribute : Attribute { }
        internal class IsPublicAttribute : Attribute { }
        internal class IsStaticAttribute : Attribute { }
        internal class IsVirtualAttribute : Attribute { }

        internal class ReturnTypeAttribute : Attribute { }
        internal class LastParameterAttribute : Attribute { }
        internal class CreateParameterAttribute : Attribute { }
    }
}
